﻿using System.Collections.Generic;
using System;
using Evolus.Mercury.Model;
using Evolus.Mercury.WebServiceReferences.HoSE;

namespace Evolus.Mercury.MarketInfoProviders.HoSE
{
    public class HoSEMarketInfoProvider : IMarketInfoProvider
    {
        private const int SYMBOL_INDEX = 0;
        private const int REFERENCE_PRICE_INDEX = 1;
        private const int CEILING_PRICE_INDEX = 2;
        private const int FLOOR_PRICE_INDEX = 3;

        private const int BUY_PRICE_1_INDEX = 4;
        private const int BUY_VOLUME_1_INDEX = 5;
        private const int BUY_PRICE_2_INDEX = 6;
        private const int BUY_VOLUME_2_INDEX = 7;
        private const int BUY_PRICE_3_INDEX = 8;
        private const int BUY_VOLUME_3_INDEX = 9;

        private const int LAST_TRADED_PRICE_INDEX = 10;
        private const int LAST_TRADED_VOLUME_INDEX = 11;
        private const int LAST_CHANGE_INDEX = 12;

        private const int SELL_PRICE_1_INDEX = 13;
        private const int SELL_VOLUME_1_INDEX = 14;
        private const int SELL_PRICE_2_INDEX = 15;
        private const int SELL_VOLUME_2_INDEX = 16;
        private const int SELL_PRICE_3_INDEX = 17;
        private const int SELL_VOLUME_3_INDEX = 18;
        
        private const int CLOSING_PRICE_INDEX = 19;
        private const int HIGH_PRICE_INDEX = 20;
        private const int LOW_PRICE_INDEX = 21;
        
        private const int TOTAL_MATCHED_VOLUME_INDEX = 22;

        private bool firstTime = true;

        public IList<Stock> GetListingStocks()
        {
            //WebServiceSoapClient client = new WebServiceSoapClient();
            //string sequenceString = client.GetSeqHO();
            //throw new Exception(sequenceString);
            //int sequence = Convert.ToInt32(sequenceString);
            //string result = client.GetUpdateHO(2098);
            return null;
        }

        public IList<StockInfo> GetChanges()
        {
            HoSTC_ServiceSoapClient serviceClient = new HoSTC_ServiceSoapClient();
            string rawMarketInfo = serviceClient.GetLiveSecurity(firstTime);
            return ParseMarketInfo(rawMarketInfo);
        }

        private IList<StockInfo> ParseMarketInfo(string rawMarketInfo)
        {            
            IList<StockInfo> stocks = new List<StockInfo>();
            if (!String.IsNullOrEmpty(rawMarketInfo))
            {
                string[] stockRows = rawMarketInfo.Split(new string[] { "**" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string stockRow in stockRows)
                {
                    string[] stockData = stockRow.Split('|');

                    StockInfo stockInfo = new StockInfo();
                    stockInfo.Date = DateTime.Now;

                    stockInfo.Stock = new Stock();
                    stockInfo.Stock.Symbol = stockData[SYMBOL_INDEX].Trim().ToUpper();

                    stockInfo.ReferencePrice = Decimal.Parse(stockData[REFERENCE_PRICE_INDEX]);
                    stockInfo.CeilingPrice = Decimal.Parse(stockData[CEILING_PRICE_INDEX]);
                    stockInfo.FloorPrice = Decimal.Parse(stockData[FLOOR_PRICE_INDEX]);

                    try
                    {
                        stockInfo.BuyPrice1 = Decimal.Parse(stockData[BUY_PRICE_1_INDEX]);
                        stockInfo.BuyVolume1 = Int32.Parse(stockData[BUY_VOLUME_1_INDEX]);
                    }
                    catch { }

                    try
                    {
                        stockInfo.BuyPrice2 = Decimal.Parse(stockData[BUY_PRICE_2_INDEX]);
                        stockInfo.BuyVolume2 = Int32.Parse(stockData[BUY_VOLUME_2_INDEX]);
                    }
                    catch { }
                    try
                    {
                        stockInfo.BuyPrice3 = Decimal.Parse(stockData[BUY_PRICE_3_INDEX]);
                        stockInfo.BuyVolume3 = Int32.Parse(stockData[BUY_VOLUME_3_INDEX]);
                    }
                    catch { }

                    stockInfo.LastTradedPrice = Decimal.Parse(stockData[LAST_TRADED_PRICE_INDEX]);
                    stockInfo.LastTradedVolume = Int32.Parse(stockData[LAST_TRADED_VOLUME_INDEX]);

                    try
                    {
                        stockInfo.SellPrice1 = Decimal.Parse(stockData[SELL_PRICE_1_INDEX]);
                        stockInfo.SellVolume1 = Int32.Parse(stockData[SELL_VOLUME_1_INDEX]);
                    }
                    catch { }
                    try
                    {
                        stockInfo.SellPrice2 = Decimal.Parse(stockData[SELL_PRICE_2_INDEX]);
                        stockInfo.SellVolume2 = Int32.Parse(stockData[SELL_VOLUME_2_INDEX]);
                    }
                    catch { }
                    try
                    {
                        stockInfo.SellPrice3 = Decimal.Parse(stockData[SELL_PRICE_3_INDEX]);
                        stockInfo.SellVolume3 = Int32.Parse(stockData[SELL_VOLUME_3_INDEX]);
                    }
                    catch { }

                    stockInfo.ClosingPrice = Decimal.Parse(stockData[CLOSING_PRICE_INDEX]);
                    stockInfo.HighPrice = Decimal.Parse(stockData[HIGH_PRICE_INDEX]);
                    stockInfo.LowPrice = Decimal.Parse(stockData[LOW_PRICE_INDEX]);

                    stockInfo.TotalMatchedVolume = Int32.Parse(stockData[TOTAL_MATCHED_VOLUME_INDEX]);

                    stocks.Add(stockInfo);
                }
            }
            return stocks;
        }        
    }
}
